package com.zm.blog.enums;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 枚举类`BlogEnum`，它包含多个不同类别的枚举值，比如状态、公开性、用户性别等
 */
public enum BlogEnum {

    /**
     * 状态
     */
    STATUS_ENABLE(1, "启用", "status"),
    STATUS_DISABLE(0, "禁用", "status"),

    /**
     * 可见性
     */
    PUBLIC(1, "所有人可见", "visibility"),
    PRIVATE(0, "仅自己可见", "visibility"),

    /**
     * 用户性别
     */
    USER_GENDER_BOY(1, "男", "gender"),
    USER_GENDER_GIRL(2, "女", "gender"),
    USER_GENDER_NONE(0, "保密", "gender"),

    /**
     * 分类类型
     */
    SORT_TYPE_BAR(0, "导航栏分类", "category"),
    SORT_TYPE_NORMAL(1, "普通分类", "category"),

    /**
     * 系统配置可见性
     */
    SYS_CONFIG_PUBLIC(2, "公开", "configVisibility"),
    SYS_CONFIG_PRIVATE(1, "私有", "configVisibility"),

    /**
     * 用户类型
     */
    USER_TYPE_ADMIN(0, "站长", "userType"),
    USER_TYPE_DEV(1, "管理员", "userType"),
    USER_TYPE_USER(2, "用户", "userType");

    private final int code;
    private final String msg;
    private final String category; // 新增分类字段

    BlogEnum(int code, String msg, String category) {
        this.code = code;
        this.msg = msg;
        this.category = category;
    }

    public int getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public String getCategory() {
        return category;
    }

    // 按分类分组获取所有枚举
    private static final Map<String, List<BlogEnum>> CATEGORY_MAP =
            Collections.unmodifiableMap(
                    Arrays.stream(values())
                            .collect(Collectors.groupingBy(BlogEnum::getCategory))
            );

    // 按分类和code获取枚举
    public static Optional<BlogEnum> getEnum(String category, int code) {
        return Optional.ofNullable(CATEGORY_MAP.get(category))
                .flatMap(list -> list.stream()
                        .filter(e -> e.code == code)
                        .findFirst());
    }

    // 按分类获取所有枚举
    public static List<BlogEnum> getEnumsByCategory(String category) {
        return CATEGORY_MAP.getOrDefault(category, Collections.emptyList());
    }

    // 按分类获取code->msg的映射
    public static Map<Integer, String> getMapByCategory(String category) {
        return getEnumsByCategory(category).stream()
                .collect(Collectors.toMap(
                        BlogEnum::getCode,
                        BlogEnum::getMsg,
                        (existing, replacement) -> existing));
    }

    // 获取所有分类列表
    public static List<String> getAllCategories() {
        return CATEGORY_MAP.keySet().stream()
                .sorted()
                .collect(Collectors.toList());
    }

    // 按code获取枚举（注意：不同分类可能有相同code值）
    public static List<BlogEnum> getEnumsByCode(int code) {
        return Arrays.stream(values())
                .filter(e -> e.code == code)
                .collect(Collectors.toList());
    }

}
